import {createStore} from 'vuex';
import {account} from "../config/env";
import website from "../config/website";
import {
    setConfig,
    getConfig,
    setArrayStorage,
    getArrayStorage,
    setStorage,
    getStorage,
    removeStorage,
} from "/src/util";

import {login, register, out, getInfo, getKey} from "/src/request/api/user";
import {engine, menu, bookmark, quick} from "/src/request/api/nav";
import {search, click} from '/src/request/api/nav';
import {icon} from '/src/request/api/icon';

const key = website.key,
    lock = website.lock,
    exId = website.exId,
    token = website.token,
    userInfo = website.info,
    config = getConfig() || website.settingConfig;

const store = createStore({
    // 状态
    state: {
        // 配置
        skin: config.skin,
        background: config.background,
        // 基础
        key: getStorage(key) || '',
        exId: getStorage(exId) || '',
        lock: getStorage(lock) || (account ? 'lock' : 'open'),     // 锁屏状态 open:解锁 / lock:锁屏
        // searchPanel: false,                                  // 搜索面板
        settingPanel: false,                                    // 设置面板
        event: '',                                              // 事件监听器 如:refresh刷新事件
        // 用户数据
        token: getStorage(token) || '',
        userInfo: getArrayStorage(userInfo) || null,
        // 数据集
        engine: getArrayStorage(website.engine) || [],
        bookmark: getArrayStorage(website.bookmark) || [],
        icon: getArrayStorage(website.icon) || [],
        menu: getArrayStorage(website.menu) || [],
        quick: getArrayStorage(website.quick) || [],
    },
    // 突变
    mutations: {
        // 设置KEY
        setKey(state, val = website.defaultKey) {
            state.key = val;
            setStorage(key, val);
        },
        // 设置EXID
        setExId(state, val = website.defaultExId) {
            state.exId = val;
            setStorage(exId, val);
        },
        // 设置Token
        setToken(state, val = '') {
            state.token = val;
            if (val) {
                setStorage(token, val);
            } else {
                removeStorage(token);
            }
        },
        // 设置lock
        setLock(state, val = true) {
            if (account) {
                let status = val ? 'lock' : 'open';
                state.lock = status;
                setStorage(lock, status);
            } else {
                let open = 'open';
                state.lock = open;
                setStorage(lock, open);
            }
        },
        // 设置用户信息
        setUserInfo(state, val = null) {
            state.userInfo = val;
            if (val) {
                setArrayStorage(userInfo, val);
            } else {
                removeStorage(userInfo);
            }
        },
        // 设置主题
        setSkin(state, value = config.skin) {
            state.skin = value;
            setConfig('skin', value);
        },
        // 设置当前主题参数
        setNowSkin(state, val = '') {
            state.skin[state.skin.value] = val;
            setConfig('skin', state.skin);
        },
        // 设置当前主题参数配置 opt 参数示例: {key: 'website内配置的key', val: '想设置的值'}
        setNowSkinConfig(state, opt) {
            state.skin[state.skin.value][opt.key] = opt.val;
            setConfig('skin', state.skin);
        },
        // 设置Background
        setBackground(state, val = config.background) {
            state.background = val;
            setConfig('background', val);
        },
        // 设置搜索面板状态
        setSearchPanel(state, val = false) {
            state.searchPanel = val;
        },
        // 设置设置面板状态
        openSettingPanel(state, val = true) {
            state.settingPanel = val;
        },
        // 设置事件触发器
        setEvent(state, val = '') {
            state.event = val;
        },
        // 设置引擎数据
        setEngine(state, val = []) {
            state.engine = val;
            setArrayStorage(website.engine, val);
        },
        // 设置书签分类数据
        setMenu(state, val = []) {
            state.menu = val;
            setArrayStorage(website.menu, val);
        },
        // 设置书签数据
        setBookmark(state, val = []) {
            state.bookmark = val;
            setArrayStorage(website.bookmark, val);
        },
        // 设置书签(点击量前20个)数据
        setQuick(state, val = []) {
            state.quick = val;
            setArrayStorage(website.quick, val);
        },
        // 设置图标数据
        setIcon(state, val = []) {
            state.icon = val;
            setArrayStorage(website.icon, val);
        },
    },
    // 行为
    actions: {
        // 登录
        login({commit}, data) {
            return new Promise((resolve, reject) => {
                login(data).then(res => {
                    let token = res.token;
                    commit('setToken', token);
                    // commit('setKey', info.keyd);
                    resolve(res);
                }).catch(err => {
                    reject(err);
                })
            })
        },
        // 注册
        register({commit}, data) {
            return new Promise((resolve, reject) => {
                register(data).then(res => {
                    resolve(res);
                }).catch(err => {
                    reject(err);
                })
            })
        },
        // 登出
        out({commit}) {
            return new Promise((resolve, reject) => {
                out().then(res => {
                    commit('setKey');
                    commit('setToken');
                    commit('setUserInfo');
                    commit('openSettingPanel', false);
                    resolve(res);
                }).catch(err => {
                    reject(err);
                })
            })
        },
        // 获取导航页Key
        getKey({commit}) {
            return new Promise((resolve, reject) => {
                getKey().then(res => {
                    commit('setKey', res.key);
                    resolve(res);
                }).catch(err => {
                    console.log(err);
                    reject(err);
                })
            })
        },
        // 获取用户信息
        getInfo({commit}, params) {
            return new Promise((resolve, reject) => {
                getInfo().then(res => {
                    commit('setUserInfo', res.info);
                    resolve(res);
                }).catch(err => {
                    console.log(err);
                    reject(err);
                })
            })
        },
        // 搜索问题
        searchQuestion({commit}, params) {
            return new Promise((resolve) => {
                search(params).then(res => {
                    resolve(res);
                }).catch(err => {
                    console.log(err);
                })
            })
        },
        // 点击书签
        clickBookmark({commit}, params) {
            return new Promise((resolve) => {
                click(params).then(res => {
                    resolve(res);
                }).catch(err => {
                    console.log(err);
                })
            })
        },
        // 获取数据(引擎)
        getEngine({commit}, refresh = false) {
            return new Promise((resolve, reject) => {
                let array = getArrayStorage(website.engine) || [];
                if (array.length && refresh == false) {
                    resolve(array);
                } else {
                    engine().then(res => {
                        let list = res.list || [];
                        commit('setEngine', list);
                        resolve(list);
                    }).catch(err => {
                        console.log(err);
                        reject(err);
                    })
                }
            })
        },
        // 获取数据(书签)
        getBookmark({commit}, refresh = false) {
            return new Promise((resolve, reject) => {
                let array = getArrayStorage(website.bookmark) || [];
                if (array.length && refresh == false) {
                    resolve(array);
                } else {
                    bookmark().then(res => {
                        let list = res.list || [];
                        commit('setBookmark', list);
                        resolve(list);
                    }).catch(err => {
                        console.log(err);
                        reject(err);
                    })
                }
            })
        },
        // 获取数据(书签分类)
        getMenu({commit}, refresh = false) {
            return new Promise((resolve, reject) => {
                let array = getArrayStorage(website.menu) || [];
                if (array.length && refresh == false) {
                    resolve(array);
                } else {
                    menu().then(res => {
                        let list = res.list || [];
                        commit('setMenu', list);
                        resolve(list);
                    }).catch(err => {
                        console.log(err);
                        reject(err);
                    })
                }
            })
        },
        // 获取数据(书签[点击量最多的20个])
        getQuick({commit}, opt) {
            return new Promise((resolve, reject) => {
                let array = getArrayStorage(website.quick) || [];
                if (array.length && opt.refresh == false) {
                    resolve(array);
                } else {
                    quick({
                        limit: opt.limit || website.quickLimit,
                    }).then(res => {
                        let list = res.list || [];
                        commit('setQuick', list);
                        resolve(list);
                    }).catch(err => {
                        console.log(err);
                        reject(err);
                    })
                }
            })
        },
        // 获取数据(图标)
        getIcon({commit}, refresh = false) {
            return new Promise((resolve, reject) => {
                let array = getArrayStorage(website.icon) || [];
                if (array.length && refresh == false) {
                    resolve(array);
                } else {
                    icon().then(res => {
                        let list = res.list || [];
                        commit('setIcon', list);
                        resolve(list);
                    }).catch(err => {
                        console.log(err);
                        reject(err);
                    })
                }
            })
        },
        // 获取全部数据
        getAllData({commit, dispatch}) {
            console.log('getAllData');
            dispatch('getEngine', true);
            dispatch('getBookmark', true);
            dispatch('getMenu', true);
            dispatch('getIcon', true);
            dispatch('getQuick', {limit: website.quickLimit, refresh: true});
        },
        // 开关设置面板
        switchSettingPanel({commit}) {
            let that = this;
            commit('openSettingPanel', !that.state.settingPanel);
        },
    },
    // 获取
    getters: {
        key(state) {
            return state.key;
        },
        exId(state) {
            return state.exId;
        },
        token(state) {
            return state.token;
        },
        skin(state) {
            return state.skin;
        },
        background(state) {
            return state.background;
        },
        event(state) {
            return state.event;
        },
    }
});

export default store;
